package responsitory;

import android.content.Context;
import android.telephony.TelephonyManager;

import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import base.MyApplcation;
import bean.ConfigBean;
import bean.MyAddressBean;
import bean.login_bean.PersonDataBean;
import interfaces.IUserInfoInterface;
import util.volley.BaseErrorListener;
import util.volley.BaseNetRepository;
import util.volley.BaseSuccessReqListener;
import util.volley.ByJsonRequest;
import util.volley.ICompleteListener;

/**
 * Created by admin on 2016/11/28.
 */

public class UserResponsitory extends BaseNetRepository implements
        IUserInfoInterface<PersonDataBean> {
    String api = "103";//接口版本号
    String loginTypeKey = "By_User_login";// 用户登录
    String register="By_User_register";//用户注册
    private String sendVertificationCode="By_SecurityCode_send";//发送验证码
    private String yanzhengVertificationCode="By_SecurityCode_verify";//验证验证码
    private String updatePass="By_User_updatePwd";//用户修改密码（手机号+验证码）
    private String updateXinxi="By_User_update";//修改个人信息
    private String modify_pass="By_User_updatePwdByOldPwd";//用户修改密码（通过旧密码）
    private String address_pass="By_Address_add";//添加收货地址
    private String address_query="By_Address_query";//查询收货地址
    private String delete_address="By_Address_delete";//删除收货地址
    private String update_address="By_Address_update";//更新收货地址
    private String renzheng="By_Worker_apply";//司机认证
    private String jigongRenZheng="By_Worker_setSkill";//技工认证
    private String weizhiUpdate="By_User_updateLatLng";//地理位置更新

    private String configApp = "By_Config_app";//获取全局配置
    private String allAll = "By_ShoppingCart_bulkAdd";//批量添加购物车
    private String moren_address = "By_Address_getDefault";//获取默认收货地址
    public UserResponsitory(ICompleteListener iCompleteListener) {
        super(iCompleteListener);
    }

    @Override
    public void login(String username, String pwd) {
        Type type = new TypeToken<PersonDataBean>() {
        }.getType();
        String apiVer = api;
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", pwd);
        map.put("device_type","android");
        map.put("country","+86");
        map.put("role","role_skilled_worker");
        map.put("device_token",getDeviceToken());
        (new ByJsonRequest.Builder<PersonDataBean>())
                .setTypeVerParamsAndReturnClass(loginTypeKey, "104", map, type)
                .requestListener(
                        new BaseSuccessReqListener<PersonDataBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 获取默认收货地址
     *
     * @param uid
     */
    @Override
    public void getMoRenAddress(String s_id, String uid) {
        Type type = new TypeToken<MyAddressBean>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("uid", uid);
        (new ByJsonRequest.Builder<MyAddressBean>())
                .setTypeVerParamsAndReturnClass(moren_address, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<MyAddressBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }
    /**
     * 批量添加商品
     *
     * @param uid
     * @param id
     * @param sku_pkid
     * @param count
     */
    @Override
    public void addMallAll(String uid, String id, String sku_pkid, String count) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("id", id);
        map.put("sku_pkid", sku_pkid);
        map.put("count", count);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(allAll, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void register(String username, String pwd, String country, String code, String from) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", pwd);
        map.put("country","+86");
        map.put("code",code);
        map.put("from",from);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(register, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 发送验证码
     * @param country +86
     * @param mobile
     * @param code_type
     */
    @Override
    public void sendVerificationCode(String country, String mobile, String code_type) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("country", country);
        map.put("mobile", mobile);
        map.put("code_type",code_type);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(sendVertificationCode, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 验证验证码
     * @param country
     * @param mobile
     * @param code
     * @param code_type
     */
    @Override
    public void YanZhengverificationCode(String country, String mobile, String code, String code_type) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("country", country);
        map.put("mobile", mobile);
        map.put("code", code);
        map.put("code_type",code_type);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(yanzhengVertificationCode, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 查询收货地址
     *
     * @param s_id 会话id[必须](登陆时保存的auth_login_code)
     * @param uid
     */
    @Override
    public void queryAddress(String s_id, String uid) {
        Type type = new TypeToken<List<MyAddressBean>>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("uid", uid);
        (new ByJsonRequest.Builder<List<MyAddressBean>>())
                .setTypeVerParamsAndReturnClass(address_query, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<List<MyAddressBean>>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 删除收货地址
     *
     * @param uid
     * @param id
     */
    @Override
    public void deleteAddress(String s_id, String uid, String id) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("uid", uid);
        map.put("id", id);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(delete_address, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }
    /**
     * 修改密码
     * @param country   国家[必须]
     * @param code      验证码[必须](itboye是万能验证码，测试其它请调用验证码接口获取)
     * @param mobile    手机号
     * @param password  新密码[必须]（6-32位）
     */
    @Override
    public void updatePass(String country, String code, String mobile, String password) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("country", country);
        map.put("code", code);
        map.put("mobile",mobile);
        map.put("password",password);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(updatePass, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     *修改个人信息
     * @param uid
     * @param types      (nickname:昵称;  sex:性别(0,1); sign:个性签名; email:邮箱； weixin：微信； company：公司； job_title：岗位；loc_country：所在国家编码； loc_area：地区)
     * @param content
     */
    @Override
    public void upadtaInformation(String uid, String types, String content) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put(types, content);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(updateXinxi, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 修改密码（通过旧密码）
     * @param uid
     * @param password
     * @param new_password
     */
    @Override
    public void modifyPass(String uid, String password, String new_password) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("password", password);
        map.put("new_password", new_password);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(modify_pass, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void addressAdd(String s_id, String uid, String contactname, String mobile, String postal_code, String province, String city, String area, String detailinfo, String defaults, String country, String country_id) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "102";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("uid", uid);
        map.put("contactname", contactname);
        map.put("mobile", mobile);
        map.put("postal_code", postal_code);
        map.put("province", province);
        map.put("city", city);
        map.put("area", area);
        map.put("detailinfo", detailinfo);
        map.put("default", defaults);
        map.put("country", country);
        map.put("country_id", country_id);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(address_pass, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }
    /**
     * 更新收货地址
     * @param id
     * @param uid
     * @param contactname
     * @param mobile
     * @param postal_code
     * @param province
     * @param city
     * @param area
     * @param detailinfo
     * @param defaults
     * @param country
     * @param country_id
     */
    @Override
    public void updateAddress(String s_id, String id, String uid, String contactname, String mobile, String postal_code, String province, String city, String area, String detailinfo, String defaults, String country, String country_id) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", s_id);
        map.put("id", id);
        map.put("uid", uid);
        map.put("contactname", contactname);
        map.put("mobile", mobile);
        map.put("postal_code", postal_code);
        map.put("province", province);
        map.put("city", city);
        map.put("area", area);
        map.put("detailinfo", detailinfo);
        map.put("default", defaults);
        map.put("country", country);
        map.put("country_id", country_id);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(update_address, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }
    @Override
    public void sijiRenZheng(String uid, String id_number, String realname, String id_certs) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("id_number", id_number);
        map.put("realname",realname);
        map.put("id_certs",id_certs);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(renzheng, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void jigongRenZheng(String uid, String skills) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("skill_ids", skills);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(jigongRenZheng, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void weiZhiUpdate(String id, String lat, String lng,String s_id) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("lat", lat);
        map.put("lng", lng);
        map.put("s_id", s_id);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(weizhiUpdate, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }
    @Override
    public void getConfigApp() {
        Type type = new TypeToken<ConfigBean>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        (new ByJsonRequest.Builder<ConfigBean>())
                .setTypeVerParamsAndReturnClass(configApp, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<ConfigBean>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    String updatePwdByPwd="By_User_updatePwdByOldPwd";//通过旧密码修改密码
    @Override
    public void updatePwdByPwd(String uid, String oldPass, String newPass) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";

        Map<String, Object> map = new HashMap<>();
        map.put("uid",uid);
        map.put("password",oldPass);
        map.put("new_password",newPass);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(updatePwdByPwd, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    public String getDeviceToken(){
        TelephonyManager tm = (TelephonyManager) MyApplcation.ctx.getSystemService(Context.TELEPHONY_SERVICE);
        String DEVICE_ID = tm.getDeviceId();
        return  DEVICE_ID;
    }
}
